Date.prototype.Format = function(format) {
	var o = {
		"M+": this.getMonth() + 1, //month
		"d+": this.getDate(), //day
		"h+": this.getHours(), //hour
		"m+": this.getMinutes(), //minute
		"s+": this.getSeconds(), //second
		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
		"S": this.getMilliseconds() //millisecond
	}
	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}

/*
var now=(new Date()).Format("yyyy-MM-dd hh:mm:ss");//当前日期
console.log((new Date()).Format("yyyy-MM-dd hh:mm:ss"));//2018-01-22 12:00:00
console.log((new Date()).Format("yyyy-MM-dd hh:mm"));   //2018-01-22 12:00
console.log((new Date()).Format("yyyy-MM-dd hh"));      //2018-01-22 12
console.log((new Date()).Format("yyyy-MM-dd"));         //2018-01-22
console.log((new Date()).Format("yyyy-MM"));            //2018-01
console.log((new Date()).Format("yyyy"));               //2018
*/

function IOSDateFormat(data) {
	return data.replace(/\.|\-/g, '/')
}


// 返回格式为xx天xx小时xx分钟
function getDuration(faultDate, completeTime) {
	var stime = Date.parse(new Date(IOSDateFormat(faultDate)));
	var etime = Date.parse(new Date(IOSDateFormat(completeTime)));
	// 两个时间戳相差的毫秒数
	var usedTime = etime - stime;
	// 计算相差的天数  
	var days = Math.floor(usedTime / (24 * 3600 * 1000));
	// 计算天数后剩余的毫秒数
	var leave1 = usedTime % (24 * 3600 * 1000);
	// 计算出小时数  
	var hours = Math.floor(leave1 / (3600 * 1000));
	// 计算小时数后剩余的毫秒数
	var leave2 = leave1 % (3600 * 1000);
	// 计算相差分钟数
	var minutes = Math.floor(leave2 / (60 * 1000));
	var time = hours + "时" + minutes + "分";
	var time = days + "天" + hours + "时" + minutes + "分";
	return time;
}


function getCurrentYMD() {
	return (new Date()).Format("yyyy-MM-dd")
}


function timestampFormat(timestamp) {
	function zeroize(num) {
		return (String(num).length == 1 ? '0' : '') + num;
	}

	var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
	var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

	var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
	var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

	var Y = tmDate.getFullYear(),
		m = tmDate.getMonth() + 1,
		d = tmDate.getDate();
	var H = tmDate.getHours(),
		i = tmDate.getMinutes(),
		s = tmDate.getSeconds();

	if (timestampDiff < 60) { // 一分钟以内
		return "刚刚";
	} else if (timestampDiff < 3600) { // 一小时前之内
		return Math.floor(timestampDiff / 60) + "分钟前";
	} else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
		return '今天' + zeroize(H) + ':' + zeroize(i);
	} else {
		var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
		if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
			return '昨天' + zeroize(H) + ':' + zeroize(i);
		} else if (curDate.getFullYear() == Y) {
			return zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
		} else {
			return Y + '年' + zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
		}
	}
}

module.exports = {
	getDuration,
	getCurrentYMD,
	timestampFormat,
	IOSDateFormat
}
